
package me.zhengjie.repository;

import me.zhengjie.domain.AppWork;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;

/**
* @website https://el-admin.vip
* @author IQW
* @date 2022-07-03
**/
public interface AppWorkRepository extends JpaRepository<AppWork, Integer>, JpaSpecificationExecutor<AppWork> {

    @Modifying
    @Query(value = "update app_work set  status = ?2 where work_id = ?1",nativeQuery = true)
    void updateStatus(Integer workId, Integer status);

    @Modifying
    @Query(value = "update app_work set work_mile = 0 , status = ?2 where work_id = ?1",nativeQuery = true)
    void reSetStatus(Integer workId, Integer status);

    @Query(value = "SELECT IFNULL(SUM(work_mile),0) FROM app_work WHERE" +
            " user_id = ?1 and status =2", nativeQuery = true)
    float selWorkMilesSum(Integer userId);

    @Modifying
    @Query(value = "select work_id from app_work where status = 2 and  user_id = ?1 order by start_time desc limit 1",nativeQuery = true)
    Integer selWorkInt(Integer userId);

    @Modifying
    @Query(value = "update app_work set status = 3 where status < 2 and end_time < ?1 ",nativeQuery = true)
    void setWorkExp(Timestamp now);

}